﻿using System;
using System.Text;
using RazakiBLL;
using RazakiBLL.Managers;
using RazakiBLL.Entities;

namespace RazakiSite.UserControls
{
    /// <summary>
    /// سفارش در این کنترل بررسی نمی شود و به فرم
    /// OrderReal.aspx
    /// انتقال پیدا کرده سات
    /// </summary>
    public partial class UCOrderEdit : BaseUserControl
    {
        public EMessageType MessageType
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (MessageType == EMessageType.offer)
                UCInfoOffer.Hide = false;
        }

        protected void BtnSend_Click(object sender, EventArgs e)
        {
            string message;
            if (!ValidateControls(out message))
            {
                UCInfoResult.Text = message;
                UCInfoResult.InfoType = EInfoType.error;
                UCInfoResult.Hide = false;

                return;
            }

            UowRazaki uowRazaki = new UowRazaki();

            string subject = TxtSubject.Text;
            string text = TxtText.Text;
            string name = TxtName.Text;
            string phone = TxtPhone.Text;
            string email = TxtEmail.Text;
            int typeId = uowRazaki.MessageTypeRepository.GetByType(MessageType.ToString()).Id;
            int permissionId = uowRazaki.MessagePermissionRepository.GetByPermission(EMessagePermission.@private.ToString()).Id;

            MessageAnonymou messageAnonymous = new MessageAnonymou
            {
                Subject = subject,
                Text = text,
                Name = name,
                Phone = phone,
                Email = email,
                TypeId = typeId,
                PermissionId = permissionId,
                LastUpdatedDate = DateTime.Now
            };

            uowRazaki.MessageAnonymousRepository.Add(messageAnonymous);
            if (uowRazaki.Save() > 0)
            {
                UCInfoResult.Text = UtilitiesUI.Sentences(ESentences.success);
                UCInfoResult.InfoType = EInfoType.success;

                SendMessage();

                ClearControls();
            }
            else
            {
                UCInfoResult.Text = UtilitiesUI.Sentences(ESentences.error);
                UCInfoResult.InfoType = EInfoType.error;
            }

            UCInfoResult.Hide = false;
        }

        private void SendMessage()
        {
            if (!string.IsNullOrWhiteSpace(TxtEmail.Text))
            {
                string name = " ";
                string[] mails = new[] { TxtEmail.Text };

                if (!string.IsNullOrWhiteSpace(TxtName.Text))
                    name = string.Format(" آقای/خانم {0} ", TxtName.Text);

                UtilitiesUI.SendEmailForRequest(name, mails);
            }
        }

        private bool ValidateControls(out string message)
        {
            string subject = TxtSubject.Text;
            string text = TxtText.Text;

            message = string.Empty;

            if (string.IsNullOrWhiteSpace(subject))
            {
                message = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "عنوان");
                return false;
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                message = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "متن");
                return false;
            }

            return true;
        }

        private void ClearControls()
        {
            TxtSubject.Text =
                TxtText.Text =
                TxtName.Text =
                TxtPhone.Text =
                TxtEmail.Text = string.Empty;
        }

    }
}